﻿Public Class FEnterPairUpTicket

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnFromClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFromClipboard.Click
        PasteFromClipboard()
    End Sub

    Private Sub PasteFromClipboard()
        If Clipboard.ContainsText Then
            Dim sText As String = Clipboard.GetText
            Dim aLines() As String = sText.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
            For Each strLine As String In aLines
                Dim iPos As Integer = strLine.IndexOf(":"c)
                If iPos > 0 Then
                    Dim strKey As String = strLine.Substring(0, iPos).ToLower
                    Dim strVal As String = strLine.Substring(iPos + 1).Trim
                    Select Case strKey
                        Case "buddy#"
                            txtBuddyNr.Text = strVal
                        Case "pincode"
                            txtPinCode.Text = strVal
                        Case "host"
                            txtHostname.Text = strVal
                        Case "port"
                            txtPort.Text = strVal
                    End Select
                End If
            Next
        End If
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Error_Clear()
        Dim iBuddyNr As Integer = 0
        If Not Integer.TryParse(txtBuddyNr.Text, iBuddyNr) Then
            Error_Set(txtBuddyNr, "Invalid buddy number!")
        End If
        Dim iPinCode As Integer = 0
        If Not Integer.TryParse(txtPinCode.Text, iPinCode) Then
            Error_Set(txtPinCode, "Pincode must be numeric!")
        ElseIf txtPinCode.Text.Trim.Length <> 8 Then
            Error_Set(txtPinCode, "Pincode must be 8 digits long!")
        End If
        If txtHostName.Text.Trim.Length = 0 Then
            Error_Set(txtHostName, "Invalid host name!")
        Else
            Try
                Dim ip As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(txtHostName.Text)
            Catch ex As System.Net.Sockets.SocketException
                Error_Set(txtHostName, ex.Message)
            End Try
        End If
        Dim iPort As Integer = 0
        If txtPort.Text.Trim.Length = 0 Then
            Error_Set(txtPort, "Invalid port!")
        Else
            If Not Integer.TryParse(txtPort.Text, iPort) Then
                Error_Set(txtPort, "Invalid port!")
            ElseIf iPort < 1 Or iPort > 65534 Then
                Error_Set(txtPort, "Port must be between 1 and 65534!")
            End If
        End If

        If mAnyError Then Return

        If Framework.Net.Manager.StartConnectAndPairUp(txtHostname.Text.Trim, iPort, iBuddyNr, txtPinCode.Text.Trim) Then
            Me.Close()
        End If
    End Sub

    Private mAnyError As Boolean = False
    Private Sub Error_Clear()
        mAnyError = False
        ErrorProvider1.Clear()
    End Sub
    Private Sub Error_Set(ByVal control As Control, ByVal text As String)
        If Not mAnyError Then
            mAnyError = True
            control.Focus()
        End If
        ErrorProvider1.SetError(control, text)
    End Sub

    Private Sub FEnterPairUpTicket_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PasteFromClipboard()
        Me.Icon = Framework.My.Resources.Buddy

    End Sub
End Class